﻿using Crm.Bussiness;
using Crm.CrmClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Crm.Views
{
    public partial class KullaniciParametreleri : ChildWindow
    {
        CrmClient.CrmServiceClient client = new CrmClient.CrmServiceClient();
        List<KullaniciYetki> tanimlar = new List<KullaniciYetki>();

        public KullaniciParametreleri()
        {
            InitializeComponent();
            client.KullaniciYetkiListeleCompleted += client_KullaniciYetkiListeleCompleted;
            client.KullaniciListeleCompleted += client_KullaniciListeleCompleted;
            this.Loaded += KullaniciParametreleri_Loaded;
            grdListe.DataSource = tanimlar;

        }

        void KullaniciParametreleri_Loaded(object sender, RoutedEventArgs e)
        {
            client.KullaniciListeleAsync();
        }

        void client_KullaniciListeleCompleted(object sender, KullaniciListeleCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                foreach (var item in e.Result)
                {
                    cmbKullanici.Items.Add(new ComboItem { Text = item.Adi, Tag = item.ID });
                }
            }
        }

        void client_KullaniciYetkiListeleCompleted(object sender, KullaniciYetkiListeleCompletedEventArgs e)
        {
            if (e.Result.Count > 0)
            {
                tanimlar.AddRange(e.Result);
            }
            else
            {
                int id = Convert.ToInt32(((ComboItem)cmbKullanici.SelectedItem).Tag);
                tanimlar.Add(new KullaniciYetki { KullaniciID = id, Deger = "##########", Parametre = "Cari kod yapısı" });
            }
            grdListe.Refresh();
            grdListe.Columns["ID"].Visible = false;
            grdListe.Columns["KullaniciID"].Visible = false;
            grdListe.Columns["Parametre"].VisibleIndex = 0;
            grdListe.Columns["Deger"].VisibleIndex = 1;
            grdListe.Columns["Deger"].AllowEditing = DevExpress.Utils.DefaultBoolean.True;
            grdListe.ColumnsAutoWidth = true;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void cmbKullanici_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbKullanici.SelectedIndex > -1)
            {
                int id = Convert.ToInt32(((ComboItem)cmbKullanici.SelectedItem).Tag);
                client.KullaniciYetkiListeleAsync(id);
            }
        }
    }
}

